package app.rci.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import app.rci.dao.UserModelDAO;
import app.rci.domain.UserData;
import app.rci.model.UserModel;
import app.rci.model.UserModelExample;
import app.rci.model.UserModelExample.Criteria;
import app.rci.service.UsersService;

@Service
public class UsersServiceImpl implements UsersService {
	@Autowired public UserModelDAO userModelDao;
	
	@Override
	public List<UserModel> getDataUser(UserModelExample userEx, UserData param){
		userEx.setOrderByClause("USER_ID ASC");
		Criteria criteria= userEx.createCriteria();
		int start= 0;
		if(Integer.parseInt(param.getStart()) > 0)
			start= Integer.parseInt(param.getStart())+1;
		int limit= Integer.parseInt(param.getLimit());
		criteria.andIdBetween(start, (start+limit));
		userEx.createCriteria();
		List<UserModel> listUser= (List<UserModel>) userModelDao.selectUserModelByExample(userEx);
		return listUser;
	}
	
	@Override
	public List<UserModel> getTotalRecord(UserModelExample userEx){
		userEx.setOrderByClause("USER_ID ASC");
		List<UserModel> listUser= (List<UserModel>) userModelDao.selectUserModelByExample(userEx);
		return listUser;
	}
	
	@Override
	public int saveDataUser(UserModelExample userEx, UserModel data){
		Criteria criteria= userEx.createCriteria();
		criteria.andUserIdEqualTo(data.getUserId());
		userEx.createCriteria();
		int result= userModelDao.updateUserModelByExampleSelective(data, userEx);
		return result;
	}

	@Override
	public int delDataUser(int id){
		int result= userModelDao.deleteUserModelByPrimaryKey(id);
		return result;
	}	
}
